/**
 * All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
 *
 *MpmPMOStandardizationRecordController
 * PMO标准化人选档案
 *
 * 修改纪录
 * 2021-03-08 版本：1.0 fanzhiming 创建。
 * @version 版本：1.0
 * @author 作者：fanzhiming
 * 创建日期：2021-03-08
 */

package com.topscomm.mpm.controller;

import com.topscomm.basic.exception.MessageException;
import com.topscomm.cbo.pojo.CboDeptEntity;
import com.topscomm.cbo.pojo.CboRoleEntity;
import com.topscomm.cbo.service.impl.CboDeptService;
import com.topscomm.mpm.MpmSystemConst;
import com.topscomm.mpm.pojo.MpmPMOStandardizationRecordEntity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.topscomm.pub.util.ConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.topscomm.mpm.controller.auto.MpmPMOStandardizationRecordControllerAuto;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;

/**
 * @author: fanzhiming
 * @date: 2021-03-08
 * @description:PMO标准化人选档案Controller
 * @modify:
 */
@RestController
@RequestMapping("mpm/pMOStandardizationRecord")
public class MpmPMOStandardizationRecordController extends MpmPMOStandardizationRecordControllerAuto implements IMpmPMOStandardizationRecordController {

	@Autowired
	private CboDeptService cboDeptService;
	/**
	 * @author: fanzhiming
	 * @date: 2021-03-08
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_name")) {
			String value = paraMap.get("qry_name").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and name like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_code")) {
			String value = paraMap.get("qry_code").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and code = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_provincename")) {
			String value = ConvertUtil.convertToString(paraMap.get("qry_provincename").toString());
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and EXISTS ( SELECT * FROM CBODEPT LEFT JOIN MPMPMOSTANDARDIZATIONRECORD m " +
						"ON INSTR( ',' || m.chargeprovinces || ',', ',' || CBODEPT.CODE || ',' ) > 0 " +
						"WHERE CBODEPT.NAME LIKE '%{0}%' AND MpmPMOStandardizationRecord.id = m.id  ) ", value);
		}
		if (paraMap.containsKey("qry_user")) {
			String value = paraMap.get("qry_user").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and (usercode like '%{0}%' or username like '%{0}%') ", value);
		}

		return sql.toString();
	}

	/**
	 * @author: fanzhiming
	 * @date: 2021-03-08
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}
    /**
     * @Description:    重写dataCodeList
     * @Author:         wuyuyang
     * @Return:         com.topscomm.pub.vo.ResponseResult<java.lang.Object>
     * @exception:
     * @Date:           19/5/2023 下午8:21
     * @Version:        1.0
     **/
	@Override
	@GetMapping(value = "/dataCodeList")
	public ResponseResult<Object> dataCodeList(@RequestParam(required = false) Map<String, Object> paraMap){
		ResponseResult<Object> temp = super.dataCodeList(paraMap);
		Map<String,List<Map<String, Object>>> codeMap = (Map<String,List<Map<String, Object>>>)temp.getResult();
		List<Map<String, Object>> MpmPurchasePatternList = datacodeCache.getCodeListMap("MpmPurchasePattern", "", "sortcode");
		codeMap.put("MpmPurchasePatternList", MpmPurchasePatternList);
		List<Map<String, Object>> mpmProjectLevelList = datacodeCache.getCodeListMap("MpmProjectLevel", "", "sortcode");
		codeMap.put("projectlevelList", mpmProjectLevelList);
		return ResponseResult.ok(codeMap);
	}



	/**
	 * @description:查询当前用户是否为PMO
	 * @author: chengengwei
	 * @date: 2021-08-31
	 * @modify:
	 */
	@GetMapping(value = "/queryPMOPermission")
	public ResponseResult<Object> queryPMOPermission(@RequestParam(required = false) Map<String, Object> paraMap) {
		List<CboRoleEntity> roleList = this.getUserBean().getRoleList();
		boolean ispmo = false;
		if (roleList != null) {
			ispmo = roleList.stream().anyMatch(item -> MpmSystemConst.PMORole.equals(item.getCode()));
		}
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("ispmo", ispmo);
		return ResponseResult.ok(resultMap);
	}
	/**
	 * @description:查询deptcommoncode对应的pmo审批人
	 * @author: houyuhang
	 * @date: 2023-02-07
	 * @modify:
	 */
	@GetMapping(value = "/queryPMOApproval")
	public ResponseResult<Object> queryPMOApproval(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			List<MpmPMOStandardizationRecordEntity> res = this.service.queryPMOApproval(paraMap);
			return ResponseResult.ok(res);
		} catch (Exception ex) {
			return this.handleControllerException(ex);
		}
	}
}

